package com.hainatong.shiphelper.utils;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;


import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.hainatong.shiphelper.widget.recycleviewdriver.ListItemDecoration;
import com.hainatong.shiphelper.widget.recycleviewdriver.MyItemDecoration;

public class RecyclerViewHelper
{


    /**
     * 流部局  LayoutManager
     * @param context
     * @return
     */
    public static FlexboxLayoutManager getFlexboxLayoutManager(Context context) {
        //设置布局管理器
        FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(context);
        //flexDirection 属性决定主轴的方向（即项目的排列方向）。类似 LinearLayout 的 vertical 和 horizontal:
        // 主轴为水平方向，起点在左端。
        flexboxLayoutManager.setFlexDirection(FlexDirection.ROW);
        //flexWrap 默认情况下 Flex 跟 LinearLayout 一样，都是不带换行排列的，但是flexWrap属性可以支持换行排列:
        // 按正常方向换行
        flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);
        //justifyContent 属性定义了项目在主轴上的对齐方式:
        // 交叉轴的起点对齐
        flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START);
        return flexboxLayoutManager;
    }


    public static void addGridLayoutManagerBy3Count(RecyclerView recyclerview , RecyclerView.ItemDecoration decoration )
    {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(recyclerview.getContext() ,3 );
        //new LinearLayoutManager(getActivity())
        recyclerview.setLayoutManager( gridLayoutManager );
        if(ObjectUtils.isNotEmpty( decoration ))
        {
            recyclerview.addItemDecoration( decoration );
        }
        else
        {
            recyclerview.addItemDecoration( new MyItemDecoration(SizeUtils.dp2px(10),SizeUtils.dp2px(10),SizeUtils.dp2px(10),SizeUtils.dp2px(10) ));
        }
        recyclerview.hasFixedSize();
    }


    public static void addGridLayoutMgrBy2Count(RecyclerView recyclerview , RecyclerView.ItemDecoration decoration )
    {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(recyclerview.getContext() ,2 );
        recyclerview.setLayoutManager( gridLayoutManager );
        if(ObjectUtils.isNotEmpty( decoration ))
        {
            recyclerview.addItemDecoration( decoration );
        }
        else
        {
            recyclerview.addItemDecoration( new MyItemDecoration(SizeUtils.dp2px(10),SizeUtils.dp2px(10),SizeUtils.dp2px(10),SizeUtils.dp2px(10) ));
        }
        recyclerview.hasFixedSize();
    }

    public static void addLinerLayoutMgr(RecyclerView recyclerview , RecyclerView.ItemDecoration decoration)
    {
        recyclerview.setLayoutManager( new LinearLayoutManager( recyclerview.getContext() ));
        recyclerview.hasFixedSize();
        if( ObjectUtils.isNotEmpty( decoration ) )
        {
            recyclerview.addItemDecoration( decoration );
        }
        else
        {
            recyclerview.addItemDecoration( new ListItemDecoration(SizeUtils.dp2px(10)));
        }
    }

    public static void addLinerLayoutMgrNoDecoration(RecyclerView recyclerview)
    {
        recyclerview.setLayoutManager( new LinearLayoutManager( recyclerview.getContext() ));
        recyclerview.hasFixedSize();
    }

}
